﻿using System;
using System.Collections;
using System.Web;

namespace TSharp.Core.Caching
{
    /// <summary>
    /// Session 通用操作类
    /// </summary>
    /// <author>
    /// tangjingbo
    /// </author>
    /// <remarks>
    /// tangjingbo at 2009-10-19 16:15
    /// TODO:缓存方法需要进行详细测试
    /// </remarks>
    public static class SessionUtil
    {
        private static ISessionState dic
        {
            get { return AopContext.GetContext().Session; }
        }　

        /// <summary>
        /// Adds the or update.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="key">The key.</param>
        /// <param name="addValueFactory">The add value factory.</param>
        /// <param name="updateValueFactory">The update value factory.</param>
        /// <returns></returns>
        public static TValue AddOrUpdate<TValue>(string key, Func<string, TValue> addValueFactory,
                                                 Func<string, TValue, TValue> updateValueFactory)
        {
            object obj = dic[key];

            if (obj == null)
                return SetValue(key, addValueFactory(key), true);
            else
            {
                var oldValue = addValueFactory(key);
                var newValue = updateValueFactory(key, oldValue);
                return SetValue(key, newValue, true);
            }


        }

        /// <summary>
        /// Gets the or add.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="key">The key.</param>
        /// <param name="valueFactory">The value factory.</param>
        /// <returns></returns>
        public static TValue GetOrAdd<TValue>(string key, Func<string, TValue> valueFactory)
        {
            object obj = dic[key];
            if (obj != null)
                return (TValue)obj;
            return SetValue(key, valueFactory(key), true);
        }　

        /// <summary>
        /// Gets the specified key.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static TValue Get<TValue>(string key)
        {
            return (TValue)dic[key];
        }

        /// <summary>
        /// Removes the specified key.
        /// </summary>
        /// <typeparam name="TValue">The type of the value.</typeparam>
        /// <param name="key">The key.</param>
        /// <returns></returns>
        public static TValue Remove<TValue>(string key)
        {
            var value = (TValue)dic[key];
            dic.Remove(key);
            return value;
        }
        private static TValue SetValue<TValue>(string key, TValue value, bool condition)
        {
            if (condition)
                dic[key] = value;
            return value;
        }
    }
}